﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.UI;

namespace MyLib.Web.UI {
	/// <summary>
	/// 
	/// </summary>
	[ParseChildren(true)]
	[Obsolete("Use ListView control instead.", true)]
	public class MyForEach : System.Web.UI.WebControls.CompositeDataBoundControl, System.Web.UI.INamingContainer {
		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[Category("Appearance")]
		[Description("当不存在绑定数据时需要显示的内容")]
		[DefaultValue("")]
		[TemplateInstance(TemplateInstance.Single)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate EmptyDataTemplate { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[Category("Appearance")]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemTemplateContent))]
		[TemplateInstance(TemplateInstance.Single)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate HeaderTemplate { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[Category("Appearance")]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemTemplateContent))]
		[TemplateInstance(TemplateInstance.Single)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate FooterTemplate { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[Category("Appearance")]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemTemplateContent))]
		[TemplateInstance(TemplateInstance.Single)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate ItemTemplate { get; set; }

		/// <summary>
		/// 
		/// </summary>
		/// <param name="writer"></param>
		public override void RenderBeginTag(HtmlTextWriter writer) {
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="writer"></param>
		public override void RenderEndTag(HtmlTextWriter writer) {
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="dataSource"></param>
		/// <param name="dataBinding"></param>
		/// <returns></returns>
		protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding) {
			var count = 0;

			var ctrlList = new List<Control>();
			if (dataSource != null) {
				foreach (var iter in dataSource) {
					ctrlList.Add(CreateItemControl(iter, count++, dataBinding));
				}
			}

			if (ctrlList.Count == 0) {
				if (EmptyDataTemplate != null) {
					EmptyDataTemplate.InstantiateIn(this);
				}
			} else {
				if (HeaderTemplate != null) {
					var c = new ItemTemplateContainer();
					HeaderTemplate.InstantiateIn(c);
					Controls.Add(c);
				}

				foreach (var ictrl in ctrlList) {
					Controls.Add(ictrl);
				}

				if (FooterTemplate != null) {
					var c = new ItemTemplateContainer();
					FooterTemplate.InstantiateIn(c);
					Controls.Add(c);
				}
			}

			if (dataBinding) {
				DataBind(false);
			}

			return count;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="dataItem"></param>
		/// <param name="index"></param>
		/// <param name="dataBinding"></param>
		/// <returns></returns>
		protected virtual Control CreateItemControl(object dataItem, int index, bool dataBinding) {
			var ictrl = new ItemTemplateContent(dataItem, index, index);

			ItemTemplate.InstantiateIn(ictrl);

			return ictrl;
		}
	} // end of MyDataList.
}
